﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace LibFriesland_Campina_CRM
{
    class DalStockChecking
    {
        //Them phieu kiem kho
        public static long addStockChecking(BizStockChecking pStockChecking)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                StockChecking stockChecking = new StockChecking();
                stockChecking.DateTimeChecking = pStockChecking.datetime_checking;
                stockChecking.DateTimePhoneChecking = pStockChecking.datetime_phone_checking;
                stockChecking.ByNA = pStockChecking.by_na;
                stockChecking.ByShop = pStockChecking.by_shop;
                stockChecking.SessionChecking = pStockChecking.session_checking;
                db.StockChecking.InsertOnSubmit(stockChecking);
                db.SubmitChanges();
                return stockChecking.StockCHeKingNumID;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Lay ID phieu kiem kho theo Sesssion
        public static long getStockCheckingNumID(string pSession_checking)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var stockChecking = db.StockChecking.Single(p => p.SessionChecking == pSession_checking);
                return stockChecking.StockCHeKingNumID;
            }
            catch(Exception ex)
            {
                return -1;
            }
        }

        #region Dung cho Webservice
        //Them phieu kiem kho boi NA
        public static int addStockCheckingbyNA(BizStockChecking pStockChecking, BizStockCheckingDetail pStockCheckingDetail)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                long currStockCheckingNumID = getStockCheckingNumID(pStockChecking.session_checking);
                if (currStockCheckingNumID == -1) //Neu chua ton tai phieu kiem kho
                    currStockCheckingNumID = pStockChecking.addStockChecking();

                //Cap nhat lai thong tin cho Chi tiet phieu kiem kho
                pStockCheckingDetail.by_stock_checking = currStockCheckingNumID;
                pStockCheckingDetail.changing_quantity = pStockCheckingDetail.current_quantity - BizProductInShop.getProductQuantityInShop(pStockCheckingDetail.by_product, pStockChecking.by_shop);
                pStockCheckingDetail.addStockCheckingDetail();

                //Cap nhat lai so luong san pham trong cua hang
                BizProductInShop.updateProductQuantityInShop(pStockCheckingDetail.by_product, pStockChecking.by_shop, pStockCheckingDetail.changing_quantity);

                //Cap nhat lai so luong san pham cua cua hang trong nhat ki hang
                BizProductInShopDiary.updateProductQuantityInShopDiary(pStockCheckingDetail.by_product, pStockChecking.by_shop, pStockChecking.datetime_phone_checking, pStockCheckingDetail.changing_quantity);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
        #endregion
    }
}
